﻿package net.jmp0.toast.entities.effects 
{
	import flash.geom.ColorTransform;
	import net.jmp0.toast.entities.bases.GameButton;
	import punk.Acrobat;
	import punk.core.Spritemap;
	/**
	 * ...
	 * @author Thodd
	 */
	public class LevelCompleteOverlay extends Acrobat{
		
		//Embedding overlay sprite
		[Embed(source = '../../res/overlay.png')] private var imgOverlay:Class;
		private var sprOverlay:Spritemap = FP.getSprite(imgOverlay, 480, 480, false, false, 100, 0, false);
		
		//talking toast
		[Embed(source = '../../res/toastFaceAnimated.png')] private var imgToastFace:Class;
		private var sprToastFace:Spritemap = FP.getSprite(imgToastFace, 100, 100, false, false, 0, 0, true);
		
		//Parameters
		private var xmlData:XML;
		private var msg:String;
		private var callback:Function;
		
		//title of the speech bubble
		public var speechTitle:GameText;
		
		/**
		 * Setting up the LevelComplete Overlay
		 * @param	xmlData the next level which will be loaded after the player has clicked "let's go"
		 * @param	msg a String that will be shown inside the overlaid messagebox
		 * @param	switchToLevel the function which is called after the player has clicked "let's go"
		 */
		public function LevelCompleteOverlay(xmlData:XML, msg:String, switchToLevel:Function) {
			this.xmlData = xmlData;
			this.msg = msg;
			this.callback = switchToLevel;
			
			setupOverlay();
			
			this.depth = -350;
			
			this.x = 130;
			this.y = 15;
			
			this.delay = (30 / 3);
			
			setupTexts();
			
			FP.world.add(this);
			
		}
		
		/**
		 * setting the alpha for the overlay sprite
		 */
		private function setupOverlay():void {
			//setup speech bubble sprite
			var colorT:ColorTransform = new ColorTransform();
			colorT.alphaMultiplier = 0.8;
			sprOverlay.colorTransform(sprOverlay.rect, colorT);
		}
		
		
		/**
		 * setting up the texts inside the overlay messagebox
		 */
		private function setupTexts():void {
			speechTitle = new GameText("level complete", x + 24, y+10, "garbage", 70, -351, true);
			new GameText(msg, x + 24, y + 80, "garbage", 22, -351, true).color = 0x800000;
			new GameText("coming up:", x + 24, y + 105, "garbage", 40, -351, true);
			new GameText("" + xmlData.@title, x + 24, y + 145, "garbage", 40, -351, true).color = 0x263466;
			new GameText("level password:", x + 24, y + 180, "garbage", 40, -351, true);
			new GameText("" + xmlData.@password, x + 24, y + 215, "garbage", 40, -351, true).color = 0x263466;
			//new GameText("let's go!", x + 207, y + 255, "garbage", 50, -351, true);
			new NextLevelButton(x + 170, y + 275, nextLevelButtonPressed).depth = -351;
		}
		
		/**
		 * callback function used by the NextLevelButton
		 * @param	caller reference to the caller of this function, most likely a NextLevelButton instance
		 */
		private function nextLevelButtonPressed(caller:GameButton):void {
			callback(xmlData);
		}
		
		
		override public function render():void {
			updateImage(sprToastFace.number);
			drawSprite(sprOverlay, 0, x, y, false, false);
			drawSprite(sprToastFace, image, x-100, y+350, false, false);
		}
		
	}

}